package com.supcon.exchangedata.service.lq;

import com.supcon.exchangedata.pojo.entity.oracle.CtKpiDay;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersDay;
import com.supcon.exchangedata.pojo.entity.mysql.operation.lq.LqDailyPitchProduction;
import com.supcon.exchangedata.repository.master.MasterKpiDayRepository;
import com.supcon.exchangedata.repository.master.MasterStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.slave7.SlaveLqOperationInfo1;
import com.supcon.exchangedata.service.CommonMethodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2023/01/18/9:58
 * @Description:
 */
@Service
@Slf4j
public class LqOperationService {

    @Resource
    private MasterKpiDayRepository masterKpiDayRepository;

    @Resource
    private MasterStatistOtherParametersDayRepository masterStatistOtherParametersDayRepository;

    @Resource
    private CommonMethodService commonMethodService;
    @Resource
    private SlaveLqOperationInfo1 slaveLqOperationInfo1;

    @Transactional
    public void LqDayOperationData() {
        LqDailyPitchProduction topData = slaveLqOperationInfo1.findTopData();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (topData != null) {
            LocalDate productDate = topData.getProductDate();
            String dataTime = timeFormatter.format(productDate);
            //li
            BigDecimal pitchProduction = topData.getPitchProduction();
            if (!StringUtils.isEmpty(pitchProduction)) {
                saveOrUpdateKpiDay("10000022264", dataTime, pitchProduction.toPlainString());
                commonMethodService.saveRedis(pitchProduction.toPlainString(),"吨",dataTime,"10000022264");
                saveOrUpdateStatistOtherParametersDay(5L, 50001L, dataTime, pitchProduction.toPlainString());
            }
        }

    }

    @Transactional
    public void LqDayOperationDataByYear(LocalDateTime startTime, LocalDateTime endTime) {
        LocalDate startTimeDate = startTime.toLocalDate();
        LocalDate endTimeDate = endTime.toLocalDate();
        List<LqDailyPitchProduction> allByProductDateBetween = slaveLqOperationInfo1.findAllByProductDateBetween(startTimeDate, endTimeDate);
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (!CollectionUtils.isEmpty(allByProductDateBetween)) {
            for (LqDailyPitchProduction topData : allByProductDateBetween) {
                if (topData != null) {
                    LocalDate productDate = topData.getProductDate();
                    String dataTime = timeFormatter.format(productDate);
                    //供水量
                    BigDecimal pitchProduction = topData.getPitchProduction();
                    if (!StringUtils.isEmpty(pitchProduction)) {
                        saveOrUpdateKpiDay("10000022264", dataTime, pitchProduction.toPlainString());
                        saveOrUpdateStatistOtherParametersDay(5L, 50001L, dataTime, pitchProduction.toPlainString());
                    }
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateKpiDay(String code, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        CtKpiDay byCodeAndDayOfMonthKey = masterKpiDayRepository.findByCodeAndDayOfMonthKey(code, dayOfMonthKey);
        if (byCodeAndDayOfMonthKey == null) {
            byCodeAndDayOfMonthKey = new CtKpiDay();
            byCodeAndDayOfMonthKey.setCode(code);
            byCodeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byCodeAndDayOfMonthKey.setLastUpdateTime(LocalDateTime.now());
            byCodeAndDayOfMonthKey.setDataType(code);

        }
        byCodeAndDayOfMonthKey.setCurrentValue(value);
        masterKpiDayRepository.save(byCodeAndDayOfMonthKey);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateStatistOtherParametersDay(Long objId, Long type, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        StatistOtherParametersDay byObjIdAndTypeAndDayOfMonthKey = masterStatistOtherParametersDayRepository.findByObjIdAndTypeAndDayOfMonthKey(objId, type, dayOfMonthKey);
        if (byObjIdAndTypeAndDayOfMonthKey == null) {
            byObjIdAndTypeAndDayOfMonthKey = new StatistOtherParametersDay();
            byObjIdAndTypeAndDayOfMonthKey.setObjId(objId);
            byObjIdAndTypeAndDayOfMonthKey.setType(type);
            byObjIdAndTypeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byObjIdAndTypeAndDayOfMonthKey.setObjType(1L);
            byObjIdAndTypeAndDayOfMonthKey.setLastTime(LocalDateTime.now());
            byObjIdAndTypeAndDayOfMonthKey.setInAvgValue(Double.valueOf(value));
            masterStatistOtherParametersDayRepository.save(byObjIdAndTypeAndDayOfMonthKey);
        } else {
            masterStatistOtherParametersDayRepository.updateDay(Double.valueOf(value), objId, dayOfMonthKey, type);
        }


    }

}
